package com.zhihuichongcheng.application.ui.task.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.bean.task.TaskBean;
import com.zhihuichongcheng.application.bean.task.TaskCommentBean;
import com.zhihuichongcheng.application.ui.task.adapter.holder.TaskCommentHolder;
import com.zhihuichongcheng.application.ui.task.adapter.holder.TaskContentHolder;
import com.zhihuichongcheng.application.ui.task.adapter.holder.TaskLogHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * task detail Created by LK on 2017/1/9.
 */
public class TaskContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int ITEM_TYPE_CONTENT = 1;
    private static final int ITEM_TYPE_LOG = 2;
    private static final int ITEM_TYPE_COMMENT = 3;

    private TaskContentHolder taskContentHolder;
    private BaseActivity activity;
    private List<TaskCommentBean> commenList = new ArrayList<>();


    public TaskContentAdapter(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder;
        switch (viewType) {
            case ITEM_TYPE_CONTENT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_task_content, parent, false);
                taskContentHolder = new TaskContentHolder(activity, view);
                holder = taskContentHolder;
                break;
            case ITEM_TYPE_LOG:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_task_comment_log, parent, false);
                holder = new TaskLogHolder(view, activity);
                break;
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listadapter_task_comment, parent, false);
                holder = new TaskCommentHolder(view, activity);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TaskContentHolder)
            taskContentHolder.setData();
        else if (holder instanceof TaskCommentHolder)
            ((TaskCommentHolder) holder).setData(commenList.get(position - 1));
        else if (holder instanceof TaskLogHolder)
            ((TaskLogHolder) holder).setData(commenList.get(position - 1));
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0)
            return super.getItemViewType(position);
        if (position == 0)
            return ITEM_TYPE_CONTENT;
        else {
            int index = position - 1;
            if (index <= commenList.size() - 1) {
                TaskCommentBean commentBean = commenList.get(index);
                if (null != commentBean) {
                    int type = commentBean.getType();
                    if (type == 1) {
                        return ITEM_TYPE_LOG;
                    } else {
                        return ITEM_TYPE_COMMENT;
                    }
                }
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return commenList.size() + 1;
    }

    public void notifyTaskTitle(String title) {
        taskContentHolder.notifyTaskTitle(title);
    }

    public void notifyTaskContent(String content) {
        taskContentHolder.notifyTaskContent(content);
    }

    public void notifyTaskLocation(String location) {
        taskContentHolder.notifyTaskLocation(location);
    }

    /**
     * 更新执行者
     */
    public void notifyExecutors(List<UserBean> result) {
        taskContentHolder.notifyExecutors(result);
    }

    public void notifyCCUsers(List<UserBean> result) {
        taskContentHolder.notifyCCUsers(result);
    }

    public TaskBean getTaskBean() {
        return taskContentHolder.getTaskBean();
    }

    public void setTaskBean(TaskBean taskBean) {
        taskContentHolder.setTaskBean(taskBean);
    }

    /**
     * 重新设置评论类型
     */
    public void reSetTaskCommentList(List<TaskCommentBean> beanList) {
        clear();
        addAll(beanList);
    }

    public void clear() {
        commenList.clear();
    }

    public void addAll(List<TaskCommentBean> tasks) {
        commenList.addAll(tasks);
    }

    /**
     * 重新设置评论类型
     */
    public void setTaskCommentList(List<TaskCommentBean> beanList) {
        addAll(beanList);
    }
}
